package com.fairytask.ui
{
	import com.fairytask.data.Modulator;
	import com.fairytask.data.ViewObject;
	import com.fairytask.engine.components.ToolTips;
	import com.fairytask.engine.components.button.Button;
	import com.fairytask.engine.components.clip.Clip;
	import com.fairytask.engine.components.group.ViewGroup;
	import com.fairytask.engine.components.table.Table;
	import com.fairytask.engine.components.table.TableEvent;
	import com.fairytask.engine.components.text.Label;
	import com.fairytask.engine.components.window.Window;
	import com.fairytask.engine.define.Filter;
	import com.fairytask.engine.define.ResConfig;
	import com.fairytask.managers.TipsManage;
	import com.fairytask.resource.StaticIni;
	import com.fairytask.tools.TipsTools;
	import com.fairytask.ui.base.FrameUI;
	import com.fairytask.ui.item.HeadItem;
	import com.greensock.TimelineLite;
	import com.greensock.TweenMax;
	
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	
	/**
	 *排行榜 
	 * @author pangj
	 * 
	 */
	public class RankUI extends FrameUI
	{
		private static var _self       :RankUI;
		private var _model              :Modulator = Modulator.instance;
		
		private static var Type_Level  :int = 1;	//等级排名
		private static var Type_Rich   :int = 2;	//财富排名
		
		//******界面组件*************
		public var rankRank     :Button;    //等级排行 按钮
		public var richRank     :Button;    //财富排行 按钮
		
		public var btnsBG        :MovieClip;
		public var listBG       :MovieClip;
		public var playBG       :MovieClip;
		
		public var tableHead    :Window;    //表头底
		public var rankClip     :Clip;      //排名 标签
		public var nameClip     :Clip;      //名称 标签
		public var levelClip    :Clip;      //等级 标签
		public var goldClip     :Clip;      //仙玉 标签
		public var tableBG      :Window;    //表格底
		public var rankTable    :Table;     //排名表格
		
		public var preBtn       :Button;    //上一页 按钮
		public var nextBtn      :Button;    //下一页 按钮
		public var pageLable    :Label;     //页签
		public var pageLabBG    :MovieClip; //页签底
		public var totalInfo    :Label;     //前100名进入排行榜标签
		
		public var playerInfoBG :Window;    //玩家属性底
		public var playerInfoLab:Label;     //玩家属性标签
		
		public var petBox       :ViewGroup; //宠物头像窗口
		
		private var eqPart      :OtherEquip; //玩家信息面板
		public var infoBG       :MovieClip; //等级底
		public var infoName     :Label;     //玩家姓名
		public var infoLevel    :Label;     //玩家等级
		
		public var nowRank      :Label;     //当前排名标签
		public var myRank       :Label;     //玩家当前排名
		public var getPrize     :Button;    //领取奖励 按钮
		//*********************
		private var _petIndex   :Array=[];  //宠物战斗位置
		private var _petModel   :Array=[];  //宠物模型
		private var isPlayer      :Boolean;   //当前选项是否是排行榜中的角色 true:角色玩家  false:宠物
		
		private var curName     :String;    //当前玩家姓名
		private var curSex      :int;       //当前玩家性别
		private var nowPage     :int = 1;   //当前页码
		private var maxPage     :int;       //总页码
		private var prizeRemain :int;       //奖励领取剩余时间
		private var rankType    :int = Type_Level;//当前排行规则
		//按钮发光
		private var bgTimeLine  :TimelineLite = new TimelineLite({repeat:-1});
		private var maskClick   :Sprite;    //不可领取奖励时显示tips
				
		public function RankUI(popUI:Boolean=true)
		{
			super(popUI);
			init();
			initEvent();
		}
		public static function getInstance():RankUI
		{
			if(_self==null)
			{
				_self=new RankUI();
			}
			return _self;
		}
		/**
		 *重写显示 
		 * @param space
		 * @param onshow
		 * 
		 */
		override public function show(space:String=MIDDLELAYER, onshow:Function=null):void
		{
			super.show(space,onshow);
			//NetMgr.sendCustomMsg(CustomToServerFlag.CLIENT_CUSTOMMSG_OPEN_RANK,rankType);
		}
		private function init():void
		{
			maskClick = new Sprite();
			maskClick.graphics.clear();
			maskClick.graphics.beginFill(0,0);
			maskClick.graphics.drawRect(525,365,120,30);
			maskClick.graphics.endFill();
			windowBG.addChild(maskClick);
			
			eqPart = new OtherEquip();
			eqPart.x = 402;
			eqPart.y = 105;
			this.addChild(eqPart);
			
			goldClip.visible = false;
			curPage = 1;
			
			bgTimeLine.append(TweenMax.to(getPrize, 0.5, {glowFilter:{color:0xf58220, alpha:0.7, blurX:20, blurY:20, strength:2, quality:1},repeat:-1,yoyo:true,startAt:{glowFilter:{color:0xFFFFFF, alpha:0.2, blurX:0, blurY:0, strength:1, quality:1}}}));
			bgTimeLine.pause();
		}
		private function initEvent():void
		{
			/*_model.addEventListener(CustomToClientEvent.SERVER_CUSTOMMSG_RANK_LIST_PAGE,getRankList);
			_model.addEventListener(CustomToClientEvent.SERVER_CUSTOMMSG_HOW_MANY_PAGE,getMaxPage);
			_model.addEventListener(CustomToClientEvent.SERVER_CUSTOMMSG_RANK_PLAYER_RANK,getMyRank);
			_model.addEventListener(CustomToClientEvent.SERVER_CUSTOMMSG_QUERY_PLAYER_MSG,getPlayInfo);
			_model.addEventListener(CustomToClientEvent.SERVER_CUSTOMMSG_CHECK_PHOTO_MSG,getPetInfo);
			_model.addEventListener(CustomToClientEvent.SERVER_CUSTOMMSG_RANK_COOL_TIME,prizeDay);*/
					
			rankTable.addEventListener(TableEvent.ROW_SELECT,sendPlayName);
			petBox.addItemsEvent(MouseEvent.CLICK,sendPetID);
			
			rankRank.addEventListener(MouseEvent.CLICK,onRankList);
			richRank.addEventListener(MouseEvent.CLICK,onRichList);
			preBtn.addEventListener(MouseEvent.CLICK,onPrePage);
			nextBtn.addEventListener(MouseEvent.CLICK,onNextPage);
			getPrize.addEventListener(MouseEvent.CLICK,onPrize);
			
			maskClick.addEventListener(MouseEvent.ROLL_OVER,showTips);
			maskClick.addEventListener(MouseEvent.ROLL_OUT,hideTips);
			getPrize.addEventListener(MouseEvent.ROLL_OVER,showTips);
			getPrize.addEventListener(MouseEvent.ROLL_OUT,hideTips);
		}
		/**
		 *按等级排名 
		 * @param evt
		 * 
		 */
		private function onRankList(evt:MouseEvent):void
		{
			rankRank.skin = "button_035";
			richRank.skin = "button_034";
			rankType = Type_Level;
			levelClip.visible = true;
			goldClip.visible  = false;
			//NetMgr.sendCustomMsg(CustomToServerFlag.CLIENT_CUSTOMMSG_OPEN_RANK,rankType);
		}
		/**
		 *按仙玉排名 
		 * @param evt
		 * 
		 */
		private function onRichList(evt:MouseEvent):void
		{
			richRank.skin = "button_035";
			rankRank.skin = "button_034";
			rankType = Type_Rich;
			levelClip.visible = false;
			goldClip.visible  = true;
			//NetMgr.sendCustomMsg(CustomToServerFlag.CLIENT_CUSTOMMSG_OPEN_RANK,rankType);
		}
		/**
		 *设置当前页码 
		 * @param val
		 * 
		 */
		private function set curPage(val:int):void
		{
			nowPage = val;
			preBtn.enabled = nowPage != 1;
			nextBtn.enabled  = nowPage != maxPage;
			pageLable.htmlStrByBorder = nowPage+"/"+maxPage;
			rankTable.clear();
		}
		
		/**
		 *获取排名列表 
		 * @param evt
		 * 
		 */
		/*private function getRankList(evt:CustomToClientEvent):void
		{
			while(evt.pList.length)
				rankTable.addRow([evt.pList.shift(),evt.pList.shift(),MoneyTools.setMoney(evt.pList.shift())]);
		}*/
		/**
		 *获取当前排名类型下的最大页码 
		 * @param evt
		 * 
		 */
		/*private function getMaxPage(evt:CustomToClientEvent):void
		{
			maxPage = evt.pList.shift();
			curPage = 1;
		}*/
		/**
		 *点击上一页 
		 * @param evt
		 * 
		 */
		private function onPrePage(evt:MouseEvent):void
		{
			if(nowPage>1)
			{
				curPage = nowPage - 1;
				//NetMgr.sendCustomMsg(CustomToServerFlag.CLIENT_CUSTOMMSG_QUERY_RANK_LIST,rankType,nowPage);
				
			}
		}
		/**
		 *点击下一页 
		 * @param evt
		 * 
		 */
		private function onNextPage(evt:MouseEvent):void
		{
			if(nowPage<maxPage)
			{
				curPage = nowPage + 1;
				//NetMgr.sendCustomMsg(CustomToServerFlag.CLIENT_CUSTOMMSG_QUERY_RANK_LIST,rankType,nowPage);
			}
		}
		/**
		 *发送玩家姓名 
		 * @param evt
		 * 
		 */
		private function sendPlayName(evt:Event):void
		{
			isPlayer = true;
			curName = (evt.currentTarget as Table).selectRow.itemArr[1].labelStr;
			eqPart.clearAll();
			clearHead();
			//NetMgr.sendCustomMsg(CustomToServerFlag.CLIENT_CUSTOMMSG_QUERY_PLAYER_MESSAGE,rankType,curName);
		}
		/**
		 *获取排行榜内玩家信息 
		 * @param evt
		 * 
		 */
		/*private function getPlayInfo(evt:CustomToClientEvent):void
		{
			petBox.getItemByIndex(0).filters = [Filter.YELLOW_INNER];
			var playId  :int = evt.pList.shift();
			curSex = evt.pList.shift();
			
			eqPart.setModelInfo(evt.pList.shift(),evt.pList.shift());
			
			var petNum  :int = evt.pList.shift();
			eqPart.roleModel = String(playId);
			showHead(playId,-1,0);
			
			for(var i:int=0;i<petNum;i++)
			{
				var petPos  :int = evt.pList.shift();
				var petId   :int = evt.pList.shift();
				showHead(petId,petPos,i+1);
			}
			equipInfo(evt.pList);
		}*/
		/**
		 *处理装备信息 
		 * @param eqInfo
		 * 
		 */
		private function equipInfo(eqInfo:Array):void
		{
			var list : Array = [];
			while(eqInfo.length)
			{
				var val  : String = eqInfo.shift();
				if(val == "*")
				{
					showEquip(list);
					list = [];
				}else
				{
					list.push(val);
				}
			}
		}
		/**
		 *显示装备 
		 * @param list
		 * 
		 */
		private function showEquip(list:Array):void
		{
			var point:Point = new Point();
			var eqPos:int = list.shift();
			//list.shift();duiXiang
			var viewObj:ViewObject = new ViewObject(null,point);
			var configId  :int = list[0];
			if(eqPos == 2 && isPlayer)
			{
				var cfgId:String = String(configId);
				cfgId = cfgId.substr(0,6)+"00";
				eqPart.WeaponModel(int(cfgId),curSex);
			}
			var itemType  :int = int(StaticIni.getIniVar(String(configId),"ItemType"));
			TipsTools.DataHandler(viewObj,list);
			eqPart.addEquip(eqPos,viewObj);
		}
			
	
		/**
		 *显示玩家及宠物头像 
		 * @param model
		 * @param pos
		 * @param index
		 * 
		 */
		private function showHead(model:int,pos:int,index:int):void
		{
			_petIndex[index] = pos;
			_petModel[index] = model;
			var head:HeadItem = new HeadItem();
			head.x = head.y = -5;
			head.source = ResConfig.pngPhotoPath + model + ResConfig.iconImg;
			petBox.getItemByIndex(index).addItem(head);
		}
		/**
		 *清除头像 
		 * 
		 */
		private function clearHead():void
		{
			_petIndex = [];
			_petModel = [];
			for(var i:int=0;i<5;i++)
			{
				petBox.getItemByIndex(i).clear();
				petBox.getItemByIndex(i).filters = null;
			}
		}
		/**
		 *发送玩家宠物ID 
		 * @param evt
		 * @param pox
		 * 
		 */
		private function sendPetID(evt:MouseEvent,pox:int):void
		{
			if(evt.currentTarget.getItem()==null)
				return;
			isPlayer = pox == 0;
			eqPart.clearAll();
			if(evt.currentTarget.getItem() is HeadItem)
			{
				//NetMgr.sendCustomMsg(CustomToServerFlag.CLIENT_CUSTOMMSG_CHECK_PHTOT,rankType,curName,_petIndex[pox]);
				eqPart.roleModel = _petModel[pox];
				for(var i:int=0;i<5;i++)
					petBox.getItemByIndex(i).filters = i==pox?[Filter.YELLOW_INNER]:null;
				
			}
		}
		/**
		 *获取玩家宠物信息 
		 * @param evt
		 * 
		 */
		/*private function getPetInfo(evt:CustomToClientEvent):void
		{
			evt.pList.shift();
			eqPart.setModelInfo(evt.pList.shift(),evt.pList.shift());
			equipInfo(evt.pList);
		}*/
		/**
		 *点击按钮，领取奖励 
		 * @param evt
		 * 
		 */
		private function onPrize(evt:MouseEvent):void
		{
			//NetMgr.sendCustomMsg(CustomToServerFlag.CLIENT_CUSTOMMSG_GET_RANK_GIFT,rankType);
		}
		/**
		 *设置领取奖励剩余时间 
		 * @param evt
		 * 
		 */
		/*private function prizeDay(evt:CustomToClientEvent):void
		{
			prizeRemain = evt.pList.shift();
			
			if(prizeRemain == 0)//可以领取奖品
			{
				getPrize.enabled = true;
				bgTimeLine.play();
				maskClick.mouseEnabled = false;
			}
			else//不可以领取奖品
			{
				bgTimeLine.stop();
				getPrize.filters = null;
				getPrize.enabled = false;
				maskClick.mouseEnabled = true;
			}
		}*/
		/**
		 *设置玩家当前排名 
		 * @param evt
		 * 
		 */
		/*private function getMyRank(evt:CustomToClientEvent):void
		{
			var rank:int = evt.pList.shift();
			myRank.htmlStrByBorder = rank<0?StaticIni.getSwfLangStr("1000000639"):String(rank);
		}*/
		/**
		 *显示领取奖励剩余时间tips
		 * @param evt
		 */
		private function showTips(evt:MouseEvent):void
		{
			if(prizeRemain == -1)
			{
				TipsManage.showTips(StaticIni.getSwfLangStr("1000000662"));
				return;
			}
			TipsManage.showTips(StaticIni.getSwfLangStrVar("1000000634",[prizeRemain]));
		}
		/**
		 *撤销tips 
		 * @param evt
		 */
		private function hideTips(evt:MouseEvent):void
		{
			ToolTips.hide();
		}
	}
}